package com.yt.base.controller.order;


import com.yt.base.controller.core.BaseController;
import com.yt.core.MsgBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.order.api.interfaces.GroupOrderPayService;
import com.yt.order.api.interfaces.GroupOrderService;
import com.yt.order.api.model.GroupOrder;
import com.yt.order.api.model.GroupOrderPay;
import com.yt.util.DateUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/order/groupOrderPay")
public class GroupOrderPayController extends BaseController {
	@Autowired
	private GroupOrderPayService groupOrderPayService;
	@Autowired
	private GroupOrderService groupOrderService;

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request, @RequestParam("idGroupOrder") String idGroupOrder) {
		ModelAndView mav = new ModelAndView("order/groupOrder/GroupOrderPayList");
		QueryFilter filter = new DynamicSQLFilterImpl();
		filter.setPagingBean(null);
		filter.addFilter("Q_idGroupOrder_L_EQ", idGroupOrder);
		filter.setSortField("payDate", true);
		List<GroupOrderPay> list = groupOrderPayService.find(filter);
		GroupOrder groupOrder = groupOrderService.get(Long.parseLong(idGroupOrder));
		mav.addObject("list", list);
		mav.addObject("groupOrder", groupOrder);
		return mav;
	}

	@RequestMapping("/del")
	@ResponseBody
	public Object multiDel(@RequestParam("id") Long id) {
		MsgBean msg = new MsgBean();
		GroupOrderPay pay = this.groupOrderPayService.get(id);
		GroupOrder order = this.groupOrderService.get(pay.getIdGroupOrder());
		order.setPaidAmount(order.getPaidAmount() - pay.getAmount());
		this.groupOrderService.update(order);
		this.groupOrderPayService.remove(id);
		return msg.returnMsg();
	}

	@RequestMapping("/save")
	@ResponseBody
	public Object save(GroupOrder groupOrder,
					   @RequestParam(value = "idGroupOrder", required = false) Long idGroupOrder,
					   @RequestParam(value = "payDate", required = true) String payDate,
					   @RequestParam(value = "amount", required = true) String amount,
					   @RequestParam(value = "comments", required = true) String comments) {
		MsgBean msg = new MsgBean();
		Date date = DateUtils.parseDate(payDate);
		GroupOrderPay pay = new GroupOrderPay();
		pay.setIdGroupOrder(idGroupOrder);
		pay.setPayDate(date != null ? date.getTime() / 1000 : 0);
		Float fAmount = 0f;
		if(NumberUtils.isNumber(amount)){
			fAmount = Float.valueOf(amount);
		}
		pay.setAmount(fAmount);
		pay.setComments(comments);
		this.groupOrderPayService.insert(pay);
		GroupOrder order = this.groupOrderService.get(idGroupOrder);
		order.setPaidAmount(order.getPaidAmount() + fAmount);
		this.groupOrderService.update(order);
		return msg.returnMsg();
	}
}
